﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;


using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools;
using Microsoft.Services.TestTools.UITesting.Html;
using MyClass.WriteToExcel;
using Microsoft.VisualStudio.TestTools.UITesting.HtmlControls;
using System.Diagnostics;
using System.IO;
using System.Configuration;

using Excel = Microsoft.Office.Interop.Excel;
using System.Collections;

namespace ControlPropertyGenerator
{
    [CodedUITest]
    public class Generator
    {
       
        List<string> myList = new List<string>();
        ExcelFileWriter<string> myExcel = new ExcelWriter<string>();
        BrowserWindow Currentbrowser = new BrowserWindow();
        static string sheetName = string.Empty;
        public void Generate()
        {
            
            Currentbrowser.SearchProperties.Add(UITestControl.PropertyNames.ClassName, "IEFrame");

            GenerateProperties(Currentbrowser.CurrentDocumentWindow);

            if (!Directory.Exists(ConfigurationManager.AppSettings["Path"]))
            {
                Directory.CreateDirectory(ConfigurationManager.AppSettings["Path"]);
            }
            sheetName=Currentbrowser.Title.Replace(" - Windows Internet Explorer", string.Empty);
            sheetName = Regex.Replace(sheetName, "[^a-zA-Z0-9]+", "");
            if (sheetName.Length > 25)
            {
                sheetName = sheetName.Substring(0, 25);
            }
            myExcel.WriteDateToExcel(ConfigurationManager.AppSettings["Path"] + "\\Controls.xlsx", myList, "A1", "C1", sheetName);
            myExcel.GenerateTemplate(ConfigurationManager.AppSettings["Path"] + "\\Controls.xlsx");
            
        }

       
       
        public void GenerateProperties(UITestControl parentControl)
        {

            foreach (UITestControl control in parentControl.GetChildren())
            {

                if (ConfigurationManager.AppSettings["ControlList"] == string.Empty)
                {
                    WriteProperty(control);
                }
                else if (ControlTypeFound(GetClassName(control)))
                {
                    WriteProperty(control);
                }
                if (control.GetChildren().Count > 0)
                {

                    GenerateProperties(control);
                }
            }
        }

        public bool ControlTypeFound(string className)
        {
            string ControlList = ConfigurationManager.AppSettings["ControlList"];

            string[] controls = ControlList.Split(',');

            foreach (string controlName in controls)
            {
                if (controlName == className)
                {
                    return true;
                }
            }
            return false;
        }

        public bool PropertyFound(string key)
        {
            string PropertyList = ConfigurationManager.AppSettings["PropertyList"];

            string[] Property = PropertyList.Split(',');

            foreach (string property in Property)
            {
                if (property == key)
                {
                    return true;
                }
            }
            return false;
        }
        public void WriteProperty(UITestControl control)
        {
            Dictionary<string, string> Property = new Dictionary<string, string>();
            StringBuilder propString = new StringBuilder();

            control.ForEachProperty((prop, val) =>
                   Property.Add(prop, val)
                   );

            foreach (string key in Property.Keys)
            {

                if (ConfigurationManager.AppSettings["PropertyList"] == string.Empty)
                {
                    if (Property[key] != null && Property[key] != string.Empty)
                    {
                        propString.Append(key);
                        propString.Append(" = ");
                        propString.Append("\"");
                        propString.Append(Property[key]);
                        propString.Append("\"");
                        propString.Append(" , ");
                    }
                }
                else if (PropertyFound(key))
                {
                    if (Property[key] != null && Property[key] != string.Empty)
                    {
                        propString.Append(key);
                        propString.Append(" = ");
                        propString.Append("\"");
                        propString.Append(Property[key]);
                        propString.Append("\"");
                        propString.Append(" , ");
                    }
                }
            }
            
            if (propString.ToString().Contains("InnerText"))
            {
                myList.Add(control.GetProperty("InnerText").ToString());
            }
            else if (propString.ToString().Contains("DisplayText"))
            {
                myList.Add(control.GetProperty("DisplayText").ToString());
            }
            else if (propString.ToString().Contains("FriendlyName"))
            {
                myList.Add(control.GetProperty("FriendlyName").ToString());
            }
            else
            {
                myList.Add(string.Empty);
            }

            myList.Add(GetClassName(control)); myList.Add(propString.ToString().TrimEnd(' ', ','));

        }

        private string GetClassName(UITestControl control)
        {
            HtmlInputButton HtmlInputButton=null;
            HtmlButton HtmlButton=null;

            switch (control.ClassName)
            {
                case "HtmlButton":
                    HtmlButton=control as HtmlButton;
                    HtmlInputButton=control as HtmlInputButton;
                    return HtmlButton == null ? "HtmlInputButton" : "HtmlButton";
                case "HtmlSelect":
                    return "HtmlComboBox";
                case "HtmlPane":
                    return "HtmlDiv";
                case "HtmlTextBox":
                    return "HtmlEdit";
                
                default:
                    return control.ClassName;
            }
        }
        public class ExcelWriter<T> : ExcelFileWriter<T>
        {
            object[,] myExcelData;
            
            public override object[] Headers
            {
                get
                {
                    object[] headerName = { "DisplayName", "Control Type", "Properties" };
                    return headerName;

                }
            }
            public override void FillRowData(List<T> list)
            {
                int listCount = list.Count;
                int count = 0;
                _list = list;

                myExcelData = new object[RowCount + 1, ColumnCount + 1];
                for (int row = 1; row <= RowCount; row++)
                {
                    for (int col = 0; col < ColumnCount; col++)
                    {
                        myExcelData[row, col] = list[count];
                        count++;
                    }
                }

            }

            public override object[,] ExcelData
            {
                get
                {
                    return myExcelData;

                }
            }
            public override int ColumnCount
            {
                get
                {
                    return 3;
                }
            }
            public override int RowCount
            {
                get
                {
                    return _list.Count / ColumnCount;
                }
            }
        }

    }
}
